﻿//  --------------------------------
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/oxite/license
//  ---------------------------------
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Oxite.Models;
using Oxite.ViewModels;
using Oxite.Infrastructure;
using Oxite.Extensions;
using Oxite.Skinning;
namespace Oxite.Results
{
    public class UnauthorizedResult : ViewResult
    {
        IUnityContainer container;
        object user;
        public UnauthorizedResult()
        {
        }
        public UnauthorizedResult(IUnityContainer container, object user)
        {
            this.container = container;
            this.user = user;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            ViewName = "Unauthorized";

            ViewData = context.Controller.ViewData;
            TempData = context.Controller.TempData;
            if (this.container != null)
            {
                OxiteContext con = container.Resolve<OxiteContext>();
                AppSettingsHelper app = container.Resolve<AppSettingsHelper>();

                OxiteViewModel model = new OxiteViewModel();
                //model.Site = new SiteViewModel(con.Site, app.GetInstanceName());
                if (this.user != null)
                {
                    model.AddModelItem(user);
                }
                ViewData.Model = model;
                ISkinResolverRegistry skinResolvers = container.Resolve<ISkinResolverRegistry>();
                SkinningTool.SetupSkinViewEngines(skinResolvers, con, context.RequestContext, ViewData, this);
            }
            
           
            base.ExecuteResult(context);

            context.HttpContext.Response.StatusDescription = "Forbidden";
            context.HttpContext.Response.StatusCode = 403;
            
        }
    }
}
